package com.lekang.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageInfo;
import com.lekang.R;
import com.lekang.ZcqEntity.NursingCancelDTO;
import com.lekang.ZcqEntity.NursingDetailDTO;
import com.lekang.ZcqEntity.NursingExecuteRecord;
import com.lekang.ZcqEntity.NursingOrderVO;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.le.service.NursingJobService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/nursing/job1")
public class NursingJobController {
    @Autowired
    private NursingJobService nursingJobService;
    @GetMapping("/list")
    public R list(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam(required = false) String oldName, @RequestParam(required = false) String nurseName, @RequestParam(required = false) String item , @RequestParam(required = false) List<String> timeRange,@RequestParam(required = false) String status ) {
        System.out.println(status);
        PageInfo<NursingOrderVO> nursingOrderVOList = nursingJobService.list(pageNum, pageSize, oldName, nurseName, item, timeRange,status);
       nursingOrderVOList.getList().forEach(i -> {
           System.out.println(i.getCreateTime());
           System.out.println(i.getExpectTime());

        });

        return R.ok(nursingOrderVOList);
    }
    @GetMapping("/items")
    public R items() {
        List<String> items = nursingJobService.items();
        System.out.println( items);

        return R.ok(items);
    }
    @GetMapping("/nurses")
    public R nurses() {
        List<String> nurses = nursingJobService.nurses();
        System.out.println( nurses);

        return R.ok(nurses);
    }
    @PutMapping("/cancel")
    public R cancel(@RequestBody NursingCancelDTO nursingCancelDTO) {
       Integer i= nursingJobService.cancel(nursingCancelDTO);
        return i > 0 ? R.ok() : R.error("取消失败");
    }
    @GetMapping("/detail")
    public R detail(@RequestParam String orderNo, @RequestParam String bedNo,@RequestParam String creator,@RequestParam String oldName) {
        NursingDetailDTO nursingDetailDTO = nursingJobService.detail(orderNo, bedNo, creator, oldName);
        return R.ok(nursingDetailDTO);
    }
    @PutMapping("/execute")
    public R execute(@RequestParam("executeTime")  String executeTime,
                     @RequestParam("executeRecord")  String executeRecord,
                     @RequestParam("oldName")  String oldName,
                     @RequestParam("itemName")  String itemName,
                     @RequestParam(value = "bedNo", required = false) String bedNo, // 可选参数
                     @RequestParam(value = "nurseName", required = false) String nurseName, // 可选参数
                     // 2. 接收文件参数
                     @RequestParam(value = "executeImage") MultipartFile executeImage
    ){
        try {
            // 3. 构建实体类
            NursingExecuteRecord record = new NursingExecuteRecord();
            record.setExecuteTime(executeTime);
            record.setExecuteRecord(executeRecord);
            record.setOldName(oldName);
            record.setItemName(itemName);
            record.setBedNo(bedNo);
            record.setNurseName(nurseName);


            // 4. 调用Service处理业务
            int result = nursingJobService.submitExecuteRecord(record, executeImage);

            // 5. 返回若依统一格式（AjaxResult = R类）
            return result > 0 ? R.ok("执行成功") : R.ok("执行失败");
        } catch (Exception e) {
            // 若依全局异常处理器会处理，此处直接返回错误信息
            return R.error("未知错误");
        }
    }
    @PutMapping("reschedule")
    public R reschedule(@RequestParam String oldName,@RequestParam String itemName,@RequestParam @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") LocalDateTime expectTime ) {
      int i=  nursingJobService.reschedule(oldName,itemName,expectTime);
        return i>0?R.ok("预约成功"):R.error("预约失败");
    }
}
